/**
 * @author zhaozhenjiang
 * 项目启动入口
 * */

package com.auto.oracle;

import java.io.File;


public class Bootstrap {

	public static void main(String[] args) throws Exception {
		String BASE_PATH = System.getProperty("user.dir");
		File license = new File(BASE_PATH + "/.license");
		String[] oraclezip = new String[]{};
		if(!license.exists()){
			System.out.println("==================In current directory [" + BASE_PATH + "] license file not exist==================");
			System.exit(0);
		}
		createParams para = CONFIG.getParams(BASE_PATH);
		//all
		//soft
		//environment
		if(para == null){
			System.out.println("==================license file error==================");
			System.exit(0);
		}
		if(para.getDbtype().equals("12.2.0.3") && para.getOsCode() == 6){
			System.out.println("==================The operating system version is too low to install==================");
			System.exit(0);
		}
		if(para.getDbtype().equals("11.2.0.1") && para.getOsCode() == 8){
			System.out.println("==================Unable to adapt to the operating system==================");
			System.exit(0);
		}
		if(para.getDbtype().equals("11.2.0.4") && para.getOsCode() == 8){
			System.out.println("==================Unable to adapt to the operating system==================");
			System.exit(0);
		}
		//如果不是创建环境
		if(!para.getInstallContent().equals("environment")){
			if(para.getDbtype().equals("11.2.0.1")){
				oraclezip = new String[]{"linux.x64_11gR2_database_1of2.zip",
										 "linux.x64_11gR2_database_2of2.zip"};
			}
			else if(para.getDbtype().equals("11.2.0.4")){
				oraclezip = new String[]{"linux.x64_112040_database.zip"};
			}
			else if(para.getDbtype().equals("12.1.0.2")){
				oraclezip = new String[]{"linuxamd64_12102_database.zip"};
			}
			else if(para.getDbtype().equals("12.2.0.1")){
				oraclezip = new String[]{"linuxx64_12201_database.zip"};
			}
			else if(para.getDbtype().equals("12.2.0.2")){
				oraclezip = new String[]{"linux.x64_180000_db_home.zip"};
			}
			else if(para.getDbtype().equals("12.2.0.3")){
				oraclezip = new String[]{"linux.x64_193000_db_home.zip"};
			}
			for(String item : oraclezip){
				license = new File(BASE_PATH + "/" + item);
				if(!license.exists()){
					System.out.println("==================In current directory [" + BASE_PATH + "] " + item + " file not exist==================");
					System.exit(0);
				}
			}
		}
		/*清理文件*/
		shellExecuter exec = new shellExecuter();
		exec.pushCommand("rm -f .install.sh");
		exec.pushCommand("rm -f .insRoot.sh");
		exec.pushCommand("rm -f .insDb.sh");
		exec.pushCommand("rm -f .db_install.rsp");
		exec.pushCommand("rm -f .listener.sh");
		exec.pushCommand("rm -f .createdb.sql");
		exec.pushCommand("rm -f .init.ora");
		exec.pushCommand("rm -f .createDb.sh");
		exec.pushCommand("rm -f /home/oracle/crdb.log");
		exec.pushCommand("rm -rf /home/oracle/database");
		exec.pushCommand("rm -f /home/oracle/.db_install.rsp");
		for(String item : oraclezip){
			exec.pushCommand("rm -f /home/oracle/" + item);
		}
		exec.pushCommand("rm -f /home/oracle/temp.log");
		exec.pushCommand("rm -f /home/oracle/.listener.sh");
		exec.pushCommand("rm -f /home/oracle/.createdb.sql");
		exec.pushCommand("rm -f /home/oracle/.init.ora");
		exec.pushCommand("rm -f /home/oracle/.createDb.sh");
		exec.run();
		installShell install = new installShell(BASE_PATH, ".install.sh", para);
		install.createFile();
		installRootShell insRoot = new installRootShell(BASE_PATH, ".insRoot.sh", para);
		insRoot.createFile();
		installDbShell insDb = new installDbShell(BASE_PATH, ".insDb.sh", para);
		insDb.createFile();
		installResponse response = new installResponse(BASE_PATH, ".db_install.rsp", para);
		response.createFile();
		createListener listener = new createListener(BASE_PATH, ".listener.sh", para);
		listener.createFile();
		createDbSql dbsql = new createDbSql(BASE_PATH, ".createdb.sql", para);
		dbsql.createFile();
		createDbPfile pfile = new createDbPfile(BASE_PATH, ".init.ora", para);
		pfile.createFile();
		createDatabase db = new createDatabase(BASE_PATH, ".createDb.sh");
		db.createFile();
		exec = new shellExecuter();
		exec.pushCommand("chmod +x " + BASE_PATH + "/.install.sh");
		exec.pushCommand(BASE_PATH + "/.install.sh");
		exec.run();
		//如果是all的时候执行创建数据库的脚本
		if(para.getInstallContent().equals("all")){
			exec = new shellExecuter();
			exec.pushCommand("chmod +x " + BASE_PATH + "/.insRoot.sh");
			exec.pushCommand(BASE_PATH + "/.insRoot.sh");
			exec.pushCommand("chmod +x " + BASE_PATH + "/.insDb.sh");
			exec.pushCommand(BASE_PATH + "/.insDb.sh");
			exec.run();
		}
		exec = new shellExecuter();
		/*清理文件*/
		exec.pushCommand("rm -f .install.sh");
		exec.pushCommand("rm -f .insRoot.sh");
		exec.pushCommand("rm -f .insDb.sh");
		exec.pushCommand("rm -f .db_install.rsp");
		exec.pushCommand("rm -f .listener.sh");
		exec.pushCommand("rm -f .createdb.sql");
		exec.pushCommand("rm -f .init.ora");
		exec.pushCommand("rm -f .createDb.sh");
		exec.pushCommand("rm -f /home/oracle/crdb.log");
		exec.pushCommand("rm -rf /home/oracle/database");
		exec.pushCommand("rm -f /home/oracle/.db_install.rsp");
		for(String item : oraclezip){
			exec.pushCommand("rm -f /home/oracle/" + item);
		}
		exec.pushCommand("rm -f /home/oracle/temp.log");
		exec.pushCommand("rm -f /home/oracle/.listener.sh");
		exec.pushCommand("rm -f /home/oracle/.createdb.sql");
		exec.pushCommand("rm -f /home/oracle/.init*.ora");
		exec.pushCommand("rm -f /home/oracle/.createDb.sh");
		exec.run();
		System.out.println("==================Run successfully==================");
		System.exit(0);
	}	

	@SuppressWarnings("unused")
	private class Decompile{
		
	}

}
